// pages/business_X/business_X.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    clothingStyleList: app.globalData.clothingStyleList,
    brand:[],
    size:[],
    colour:[],
    X_brand: '',
    X_size: '',
    X_colour: '',
    clothingStyleList:[],
    isMb:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getList(options.id)
  },
  getList(e){
    var that =this
    wx.request({
      url: app.globalData.api + 'designDiagram/' + e,
      data: {},
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res)
        for (let i in res.data.data.brand.split(',')){
          for (let j in res.data.data.size.split(',')){
            for (let k in res.data.data.color.split(',')){
              that.data.clothingStyleList.push({
                styleNumber: res.data.data.styleNumber,
                styleBrand: res.data.data.brand.split(',')[i],
                styleMeasure: res.data.data.size.split(',')[j],
                styleColor: res.data.data.color.split(',')[k],
              })
            }
          }
        }
        console.log(that.data.clothingStyleList)
        that.setData({
          list: res.data.data,
          brand: res.data.data.brand.split(','),
          size: res.data.data.size.split(','),
          colour: res.data.data.color.split(','),
          clothingStyleList: that.data.clothingStyleList
        })
      }
    })
  },
  add(){
    this.setData({
      isMb:true
    })
  },
  deMb(){
    this.setData({
      deMb: false
    })
  },
  dele(e){
    this.data.clothingStyleList.splice(e.currentTarget.dataset.index,1)
    this.setData({
      clothingStyleList: this.data.clothingStyleList
    })
  },
  brand_x(e){
    console.log(e)
    this.setData({
      X_brand:e.currentTarget.dataset.cont
    })
  },
  colour_x(e) {
    this.setData({
      X_colour: e.currentTarget.dataset.cont
    })
  },
  size_x(e) {
    this.setData({
      X_size: e.currentTarget.dataset.cont
    })
  },
  formSubmit(e){
    if (this.data.X_brand == '' || this.data.X_brand == '' || this.data.X_brand == '' || e.detail.value.num==''){
      wx.showModal({
        title: '提示',
        content: '请填写完整信息'
      })
      return
    }
    var obj={
      styleNumber: this.data.list.styleNumber,
      styleBrand: this.data.X_brand,
      styleMeasure: this.data.X_size,
      styleColor: this.data.X_colour,
      preOrderQuantity:e.detail.value.num
    }
    this.data.clothingStyleList.push(obj)
    console.log(this.data.clothingStyleList)
    this.setData({
      X_brand:'',
      X_size:'',
      X_colour:'',
      clothingStyleList: this.data.clothingStyleList,
      isMb:false,
    })
  },
  subm(){
    for (let i in this.data.clothingStyleList){
      if (this.data.clothingStyleList[i].preOrderQuantity == '' || this.data.clothingStyleList[i].preOrderQuantity == undefined){
        wx.showModal({
          title: '提示',
          content: '请填写完整数量',
        })
        return
      }

    }
    var data={
      clothingStyleList: this.data.clothingStyleList
    }
    app.req('businessOrderForm/add', data,'POST',res=>{
      console.log(res)
      if(res.data.code == 200){
        wx.showToast({
          title: res.data.msg,
        })
        setTimeout(()=>{
          wx.reLaunch({
            url: '/pages/business/business?current=1'
          })
        }, 1500)
      }else{
        wx.showModal({
          title: '提示',
          content: res.data.errorMsg,
        })
      }
    })
  },
  num(e){
    console.log(e)
    this.data.clothingStyleList[e.currentTarget.dataset.inde].preOrderQuantity = e.detail.value
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})